<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/24
 * Time: 16:00
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Auth extends AdminController
{

    //登录
    public function login(Request $request)
    {
        $username = $request->param('username');
        $password = $request->param('password');
        $auth = \app\admin\library\Auth::instance();
        $auth->login($username,$password);
        return ['code'=>200];
    }

    //添加员工
    public function addStaff(Request $request)
    {
        $staff = $request->only(['username','password','role_id','phone','email']);
        Db::table('rbac_staff')->insert($staff);
        return ['code'=>200];
    }

    //删除员工
    public function delSatff(Request $request)
    {
        $id = $request->param('id');
        Db::table('rbac_staff')->delete($id);
        return ['code'=>200];
    }

    //修改员工
    public function updateStaff(Request $request)
    {
        $staff = $request->only(['id','password','role_id','phone','email']);
        $staff['password'] = md5($staff['password']);
        Db::table('rbac_staff')->where('id',$staff['id'])->update($staff);
        return ['code'=>200];
    }

    //添加节点
    public function addNode(Request $request)
    {
       $node =  $request->only(['parent_id','name','sort','path','is_access','position','is_show']);
       Db::table('rbac_node')->insert($node);
       return ['code'=>200];
    }

    //删除节点
    public function delNode(Request $request)
    {
        $id = $request->param('id');
        Db::table('rbac_node')->delete($id);
        return ['code'=>200];
    }

    //修改节点
    public function updateNode(Request $request)
    {
        $node =  $request->only(['id','parent_id','name','sort','path','is_access','position','is_show']);
        Db::table('rbac_node')->where('id',$node['id'])->update($node);
        return ['code'=>200];
    }



    //添加角色
    public function addRole(Request $request)
    {
        $role = $request->only(['parent_id','code','name']);
        Db::table('rbac_role')->insert($role);
        return ['code'=>200];
    }

    //删除角色
    public function delRole(Request $request)
    {
        $id = $request->param('id');
        Db::table('rbac_role')->delete($id);
        return ['code'=>200];
    }

    //修改角色
    public function updateRole(Request $request)
    {
        $role = $request->only(['id','parent_id','code','name']);
        Db::table('rbac_role')->where('id',$role['id'])->update($role);
        return ['code'=>200];
    }


    //添加权限
    public function addAuth(Request $request)
    {
        $auth = $request->only(['role_id','node_id']);
        Db::table('rbac_role_node')->insert($auth);
        return ['code'=>200];
    }

    //删除权限
    public function delAuth(Request $request)
    {
        $id = $request->param('id');
        Db::table('rbac_role_node')->delete($id);
        return ['code'=>200];
    }
}